package com.xingkeduo.business.user.domain;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.user.service.*;
import com.xingkeduo.pojo.dto.PnpLogDTO;
import com.xingkeduo.pojo.enums.ConsumerStatusEnums;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/6/19 10:57
 **/
@Slf4j
@Component
public class PnpLogDomain {

    @Resource
    private CraftsmanService craftsmanService;
    @Resource
    private ConsumerService consumerService;
    @Resource
    private CraftsmanSocialService craftsmanSocialService;
    @Resource
    private ConsumerPrivateService consumerPrivateService;
    @Resource
    private FeignDomain feignDomain;
    @Resource
    private ConsumerSmsService smsService;


    /**
     * 发型师联系用户 虚拟号码
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/6/19 14:22
     */
    public String requestCallConsumer(PnpLogDTO pnpLogDTO) throws Throwable {
        log.info("虚拟号码拨打请求参数:pngLogQO[{}]", JSON.toJSONString(pnpLogDTO));

        if (0L == pnpLogDTO.getAdminId()) {
            throw new DataException(DataException.Type.NULL, "发型师标识");
        }
        if (0L == pnpLogDTO.getUserId()) {
            throw new DataException(DataException.Type.NULL, "用户标识");
        }
        if (0L == pnpLogDTO.getShopId()) {
            throw new DataException(DataException.Type.NULL, "店铺标识");
        }
        if (StringUtils.isEmpty(pnpLogDTO.getUserPhone())) {
            throw new DataException(DataException.Type.NULL, "用户手机号");
        }
        if (StringUtils.isEmpty(pnpLogDTO.getAdminPhone())) {
            throw new DataException(DataException.Type.NULL, "发型师手机号");
        }
        if (pnpLogDTO.getUserPhone().equals(pnpLogDTO.getAdminPhone())) {
            throw new DataException(DataException.Type.DEFAULT, "手机号不能相同");
        }
        CraftsmanTO craftsmanTO = selectCraftsmanById(pnpLogDTO.getAdminId());
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(craftsmanTO.getId());
        if (null == socialTO) {
            throw new DataException(DataException.Type.NULL, "发型师记录");
        }
        if (null != socialTO.getPhone() && pnpLogDTO.getAdminPhone().equals(socialTO.getPhone())) {
            ConsumerTO consumerTO = selectConsumerById(pnpLogDTO.getUserId());

            ConsumerPrivateTO privateTO = consumerPrivateService.selectByPrimary(consumerTO.getId());
            if (null == privateTO) {
                throw new DataException(DataException.Type.NULL, "用户记录");
            }
            if (null == privateTO.getPhone() || !pnpLogDTO.getUserPhone().equals(privateTO.getPhone())) {
                throw new DataException(DataException.Type.DEFAULT, "用户手机号不匹配");
            }
        } else {
            throw new DataException(DataException.Type.DEFAULT, "发型师手机号不匹配");
        }
        long cityId = feignDomain.getCityId(pnpLogDTO.getShopId());
        pnpLogDTO.setCityId(cityId);
        String virtualPhone = feignDomain.callUserPhone(pnpLogDTO);
        return virtualPhone;
    }


    private CraftsmanTO selectCraftsmanById(long id) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(id);
        if (null == craftsmanTO) {
            throw new DataException(DataException.Type.NULL, "发型师信息");
        }
        if (craftsmanTO.getStatus() == CraftsmanTO.Status.LEAVE || craftsmanTO.getIsDelete() == YesNoEnum.YES) {
            throw new DataException(DataException.Type.DEFAULT, "发型师已离职");
        }
        if (craftsmanTO.getIsDelete() == YesNoEnum.YES) {
            throw new DataException(DataException.Type.FAIL, "");
        }
        return craftsmanTO;
    }


    private ConsumerTO selectConsumerById(long id) throws Throwable {
        ConsumerTO consumerTO = consumerService.selectByPrimary(id);
        if (null == consumerTO) {
            throw new DataException(DataException.Type.NULL, "用户信息");
        }
        if (consumerTO.getfStatus() == ConsumerStatusEnums.BLACK_LIST) {
            throw new DataException(DataException.Type.DEFAULT, "用户处于黑名单状态");
        }
        if (consumerTO.getIsDelete() == YesNoEnum.YES) {
            throw new DataException(DataException.Type.FAIL, "");
        }
        return consumerTO;
    }

//    public void testFescar(VendorTable vendorTable) throws Throwable{
//        feignDomain.testFescar(vendorTable);
//        ConsumerSmsTO sms = new ConsumerSmsTO();
//        sms.setIsDelete(YesNoEnum.NO);
//        sms.setSmsType("1");
//        sms.setSmsCode("123");
//        sms.setPhone("123");
//        smsService.insert(sms);
//        throw new DataException(DataException.Type.FAIL, "");
//    }
}
